<?php
    /**
    * Session.php
    * 
    * @author Osuvaldo Ramos <xdracox@gmail.com>
    * @copyright Copyright (c) 2008, Osuvaldo Ramos
    * @license http://www.opensource.org/licenses/bsd-license.php
    * @package Atman
    */
    
    /**
    * A thin wrapper class for PHP sessions. It handles the creation and
    * destruction of the session through a singleton pattern. It also
    * allows for setting session variables.
    * @package Atman
    */
    class AT_Session
    {
        /**
        * Variable used for the singleton pattern. A single instance of the 
        * AT_Session class is stored.
        * @var AT_Session
        */
        private static $instance;
        
        /**
        * The session ID that PHP assigns.
        * @var string
        * @static
        */
        private static $sessionID;
        
        /**
        * Creates or returns the existing AT_Session instance.
        * @return AT_Session
        * @static
        */
        public static function singleton()
        {
            if ( !isset(self::$instance) )
            {
                $className = __CLASS__;
                self::$instance = new $className;
            }
            
            return self::$instance;
        }
        
        /**
        * Starts a session and stores the session ID.
        */
        private function __construct()
        {
            session_start();
            $this->sessionID = session_id();
        }
        
        /**
        * Returns the session ID of this session.
        * @return string
        */
        public function getSessionID()
        {
            return $this->sessionID;
        }
        
        /**
        * Destroys the session and clears all session information.
        * @return void
        */
        public function destroy()
        {
            $_SESSION = array();
            
            if ( isset($_COOKIE[session_name()]) )
            {
                setcookie(session_name(), '', time()-42000, '/');
            }
            
            session_destroy();
        }
        
        /**
        * Disable PHP's cloning for this session class.
        */
        public function __clone()
        {
            trigger_error('Clone is not allowed for '.__CLASS__, E_USER_ERROR);
        }
        
        /**
        * Returns the requested session variable.
        * @param string $var The name of the session variable.
        * @return mixed
        */
        public function get($var)
        {
            if ( isset($_SESSION[$var]) )
            {
                return $_SESSION[$var];
            }
            else
            {
                return NULL;
            }
        }
        
        /**
        * Provides access to set session variables.
        * @param string $var The session variable name.
        * @param mixed $val The value for the session variable.
        * @return void
        */
        public function set($var, $val)
        {
            $_SESSION[$var] = $val;
        }
        
        /**
        * Unsets session data variables.
        * @param string $var The name of the session variable to unset.
        * @return void
        */
        public function deset($var)
        {
            unset($_SESSION[$var]);
        }
        
        /**
        * Finishes off the session.
        */
        public function __destruct()
        {
            session_write_close(); 
        }
    }
?>
